﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using com.tiyma.components;
using com.tiyma.common;
using com.tiyma.model;
using System.Collections.Generic;
using System.Text;

namespace com.tiyma.web
{
    public partial class BookList : BasePage
    {
        const string m_no_description="本书没收录到简介，却不能隐晦它内容的精彩，点击看看，或许是一本精彩佳作。";
        const string m_template = "<li class=\"clearfix\"><h3><a  target=\"_blank\" href=\"/book/{0}/\">{1}</a>/<a href=\"/bookList/2/{2}/1/\">{3}</a>/<a href=\"/bookList/3/{4}/1/\">{5}</a></h3><div class=\"clear\"></div><dl class=\"clearfix\"><dt>简&nbsp;&nbsp;&nbsp;&nbsp;介：</dt><dd>{6}</dd></dl><div class=\"clear\"></div><dl class=\"clearfix\"><dt>最新章节：</dt><dd><a href=\"/{10}/{0}/{7}/\" target=\"_blank\">{8}</a> </dd></dl><div class=\"clear\"></div><dl class=\"clearfix\"><dt>更新时间：</dt><dd>{9}</dd></dl><div class=\"clear\"> </div></li>";
                  
        const string m_template_seo = "<li class=\"clearfix\"><h3><a target=\"_blank\" href=\"{0}\">" +
                                "{1}</a></h3>" +

                                "<div class=\"clear\"></div>" +
                                "<dl class=\"clearfix\"> <dt>简&nbsp;&nbsp;&nbsp;&nbsp;介：</dt><dd>{2}</dd></dl>" +
                                "<div class=\"clear\"></div>" +
                                "<dl class=\"clearfix\"><dt>更新时间：</dt><dd>{3}</dd></dl><div class=\"clear\"></div></li>";

        public string m_cur_read_list = string.Empty;

        public string m_data_list = string.Empty;

        public string m_page_list = string.Empty;

        public string m_key_word = string.Empty;

        //关键字
        string m_page_tile = string.Empty;
        string m_page_key_word = string.Empty;
        string m_page_mata_content = string.Empty;

        private const string m_right_recommend_book = "<li {3}><em>{0}</em><a href=\"/book/{1}/\" target=\"_blank\">{2}</a></li>";
        int m_search_type = 4;
        int m_page_index = 1;
        int m_page_size = 10;

        BookManager m_book_mgr = new BookManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            m_key_word = Request["k"];

            if (!int.TryParse(Request["p"], out m_page_index))
                m_page_index = 1;

            if (!int.TryParse(Request["t"], out m_search_type))
                m_search_type = 4;

            BindSearchList(m_key_word);

            BindRecommendBook();

        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (this.Master is Common)
            {
                
                Common master = (Common)this.Master;
              
                master.SetCssFile("/style/main.css");

                master.Page.Title = m_page_tile;

                master.SetPageMetaKeyword(m_page_key_word);

                master.SetPageMetaDescription(m_page_mata_content);

            }
        }

        /// <summary>
        /// 获取推荐小说
        /// </summary>
        private void BindRecommendBook()
        {
            StringBuilder t_sb = new StringBuilder();

            try
            {
                List<BookInfo> t_list = null;

                if (m_search_type == 3)
                {
                    t_list = m_book_mgr.GetRecomendBookByCategory(m_key_word, 10,Request);
                    m_page_tile = string.Format("{0}小说频道,各类{0}小说尽在免费小说体验码小说网，提供{0}小说阅读,下载", m_key_word);
                    m_page_key_word = string.Format("{0}小说阅读，{0}小说下载尽在体验码小说网",m_key_word);
                    m_page_mata_content =string.Format("读{0}小说尽在体验码小说网。本站提供{0}小说排行榜,{0}小说阅读、下载。以及{0}小说最新更新章节和{0}小说章节列表,www.tiyma.com",m_key_word);
                
                }
                else
                {
                    t_list = m_book_mgr.GetRightRecommendBook();
                }

                int index = 1;
                t_sb.Append("<div class=\"box box_height_2\">");
                t_sb.Append("<div class=\"box_tt\"><h2>推荐小说</h2></div>");
                t_sb.Append("<div class=\"box_con\"><div class=\"list_col2\">");
                t_sb.Append("<ol class=\"ranklist ranklist_txt\">");
                if (t_list != null && t_list.Count > 0)
                {
                    foreach (BookInfo t_book in t_list)
                    {
                        t_sb.AppendFormat(m_right_recommend_book
                            , index
                            , t_book.BookId
                            , t_book.BookName
                            , (index == 1 || index == 2 || index == 3 ) ? "class=\"top\"" : ""

                           );
                        index++;
                        if (index == 11)
                        {
                            break;

                        }
                    }
                }
                t_sb.Append("</ol></div></div></div>");
                lblRightRecommend.Text = t_sb.ToString();
            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定右边推荐失败" + ex.ToString());
                RedirectErrorPage();
            }
        }


        private void BindSearchList(string i_word)
        {
          

            Consts.SearchType t_search_type = Consts.SearchType.General;

            if (m_search_type == 1)
            {
                t_search_type = Consts.SearchType.BookName;
                m_page_tile = string.Format("小说{0}最新章节免费阅读尽在体验码小说网", m_key_word);
            }
            if (m_search_type == 2)
            {
                t_search_type = Consts.SearchType.AuthorName;
                m_page_tile = string.Format("作者{0}的小说列表尽在体验码小说网", m_key_word);
            }
            if (m_search_type == 3)
            {
                m_page_tile = string.Format("{0}小说频道,各类{0}小说尽在免费小说体验码小说网，提供{0}小说阅读,下载", m_key_word);
                t_search_type = Consts.SearchType.Category;
            }

            try
            {
                int o_total_count = 0;

                List<BookInfo> t_list = m_book_mgr.SearchBook(Request, i_word, t_search_type, m_page_index, m_page_size, out o_total_count);

                //从自己库存搜索到了小说信息
                if (t_list != null && t_list.Count > 0)
                {
                    StringBuilder t_sb = new StringBuilder();

                    if (t_search_type == Consts.SearchType.BookName)
                    {
                        m_page_tile = string.Format("{0},及小说作者{1}的{0}最新章节尽体验码小说网", t_list[0].BookName,t_list[0].AuthorName);
                        m_page_key_word=string.Format("{0},最新章节阅读，下载及作者{1}，分类{2}更新尽在体验码小说网", t_list[0].BookName, t_list[0].AuthorName , t_list[0].BookCategory);
                        m_page_mata_content = string.Format("{0}小说阅读,体验码小说网提供{0}最新章节及章节列表,{1}小说{0}是作者{2}创作,{0}最新更新{0}小说章节,最新更新尽在体验码小说网",
                        t_list[0].BookName, t_list[0].BookCategory, t_list[0].AuthorName);

                    }

                    StringBuilder t_author_book_sb = new StringBuilder();

                    foreach (BookInfo t_book in t_list)
                    {
                        if (t_search_type == Consts.SearchType.AuthorName)
                        {
                            t_author_book_sb.AppendFormat("{0},", t_book.BookName);
                        }

                        t_sb.AppendFormat(m_template,  
                            t_book.BookId,
                            GetHightWord(string.Format("{0}，{1}", t_book.BookName, t_book.SiteName), i_word),
                            HttpUtility.UrlEncode(t_book.AuthorName),
                            GetHightWord(t_book.AuthorName, i_word), 
                            HttpUtility.UrlEncode(t_book.BookCategory), 
                            GetHightWord(t_book.BookCategory, i_word),
                            SystemUtil.GetSubBookDescription(string.IsNullOrEmpty( t_book.Description)?m_no_description:t_book.Description, i_word, 200),
                           t_book.LastUpdateChapterId,
                           GetHightWord(SystemUtil.ChangeChapterName( t_book.LastUpdateChapterName), i_word) , 
                            t_book.LastUpdateTime.ToString("yyyy-MM-dd"),
                            t_book.IsDownLoad==Consts.ExistChapterContent?"reader":"chapter"
                           );
                    }

                    if (t_search_type == Consts.SearchType.AuthorName)
                    {
                        m_page_tile = string.Format("{0}的小说{1}最新章节尽体验码小说网",m_key_word,t_author_book_sb.ToString().Trim(','));
                        m_page_key_word = string.Format("{0},{1}最新章节阅读，下载更新尽在体验码小说网", m_key_word,t_author_book_sb.ToString().Trim(','));
                        m_page_mata_content = string.Format("{0}作者的小说{0}最新章节及章节列表,{1}是作者{0}创作,{1}最新更新{1}小说章节,最新更新尽在体验码小说网",
                        m_key_word,t_author_book_sb.ToString().Trim(','));
                    }

                    m_data_list = t_sb.ToString();

                    if (o_total_count > m_page_size)
                    {
                        m_page_list = GetPageHtml(m_page_index, m_page_size, o_total_count, string.Format("/bookList/{1}/{0}/", HttpUtility.UrlEncode(i_word), m_search_type));
                    }
                }
                //从搜索引擎搜索小说信息
                else
                {
                    SEOManager t_seo_mgr = new SEOManager();

                    t_list = t_seo_mgr.BaiDuSearch(i_word);

                    if (t_list != null)
                    {
                        StringBuilder t_sb = new StringBuilder();
                        foreach (BookInfo t_book in t_list)
                        {
                            t_sb.AppendFormat(m_template_seo, t_book.BookChapterListUrl, t_book.BookName, SystemUtil.GetSubBookDescription(string.IsNullOrEmpty(t_book.Description) ? m_no_description : t_book.Description, i_word, 200), t_book.LastUpdateTime.ToString("yyyy-MM-dd"));
                        }

                        m_data_list = t_sb.ToString();
                    }
                }



            }
            catch (Exception ex)
            {
                LogHelper.Error("绑定搜索结果失败" + ex.ToString());
            }
        }

        private string GetHightWord(string i_text, string i_word)
        {
            foreach (char t_ch in i_word)
            {
                i_text = i_text.Replace(t_ch.ToString(), string.Format("<em>{0}</em>", t_ch));
            }

            return i_text;
        }
    }
}
